package com.combetter.changxing.viewmodel;

import android.app.Application;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.combetter.changxing.MyApp;
import com.combetter.changxing.R;
import com.combetter.changxing.db.entity.Record;
import com.combetter.changxing.net.service.APIService;
import com.combetter.changxing.util.AppManager;
import com.combetter.changxing.util.LogUtil;
import com.combetter.changxing.util.ToastUtils;
import com.combetter.changxing.viewmodel.base.BaseRecyclerViewModel;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/12.
 */

public class MineRecordViewModel extends BaseRecyclerViewModel<String> {
    public List<Record> records = new ArrayList<>();

    public MineRecordViewModel(@NonNull Application application) {
        super(application, R.layout.activity_mine_record_item);
    }

    public void back(View view) {
        AppManager.finishCurrentActivity();
    }

    /**
     * 获取吸烟记录
     */
    public void selectUidSmokeSum(SmartRefreshLayout refreshLayout) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", MyApp.getInstance().getUserInfo().getUid());
        LogUtil.e("获取吸烟记录参数：", map);
        mHttpRequest.createService(APIService.class)
                .selectUidSmokeSum(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> {
                    if (MyApp.getInstance().getDatabase() != null && MyApp.getInstance().getDatabase().recordDao() != null) {
                        List<Record> recordList = MyApp.getInstance().getDatabase().recordDao().findRecordByUid(MyApp.getInstance().getUserInfo().getUid());
                        if (recordList.size() > 0) {
                            clear();
                        }
                        for (Record record : recordList) {
                            if (!TextUtils.isEmpty(record.getGoodsName())) {
                                items.add(record);
                            }
                        }
                    }
                    refreshLayout.finishRefresh();
                })
                .subscribe(httpResult -> {
                    refreshLayout.finishRefresh();
                    LogUtil.e("获取吸烟记录返回：", httpResult);
                    if (httpResult.getCode() == 200) {
                        JSONObject jsonObject = new JSONObject(new Gson().toJson(httpResult.getData()));
                        String resultData = jsonObject.getString("list");
                        records.clear();
                        records = new Gson().fromJson(resultData, new TypeToken<List<Record>>() {
                        }.getType());
                        clear();
                        if (records != null && records.size() > 0 && MyApp.getInstance().getDatabase() != null && MyApp.getInstance().getDatabase().recordDao() != null) {
                            MyApp.getInstance().getDatabase().recordDao().deleteRecordByUid(MyApp.getInstance().getUserInfo().getUid());
                        }
                        for (Record record : records) {
                            if (!TextUtils.isEmpty(record.getGoodsName())) {
                                items.add(record);
                                record.setUid(MyApp.getInstance().getUserInfo().getUid());
                                MyApp.getInstance().getDatabase().recordDao().insertSingle(record);
                            }
                        }

                    } else {
                        ToastUtils.error(httpResult.getMsg());
                    }
                });
    }
}
